﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;

namespace CampusMarket
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        AxMapControl mapControl;
        AxToolbarControl mapToolbar;
        AxTOCControl toc;
        public static int mapControlStatus = 0;   // 0-空闲 1-选点
        public static Window coworkingWnd = null; // 用于不同窗口交互暂存子窗体
        public MainWindow()
        {
            InitializeComponent();
            CreateMapControl();
        }

        private void CreateMapControl()
        {
            mapControl = new AxMapControl();
            mapHost.Child = mapControl;
        }

        private void CreateMapToolbarControl()
        {
            mapToolbar = new AxToolbarControl();
            toolbarHost.Child = mapToolbar;
        }

        private void CreateTocControl()
        {
            toc = new AxTOCControl();
            tocHost.Child = toc;
            toc.SetBuddyControl(mapControl);
        }

        private void SetMapProperties()
        {
            mapControl.Dock = System.Windows.Forms.DockStyle.None;
            mapControl.BackColor = System.Drawing.Color.FromArgb(233, 233, 233);
        }

        private void SetToolbarItems()
        {
            mapToolbar.SetBuddyControl(mapControl);
            List<string> itemUidList = new List<string>();
            itemUidList.Add("esriControls.ControlsOpenDocCommand");
            itemUidList.Add("esriControls.ControlsSaveAsDocCommand");
            itemUidList.Add("esriControls.ControlsMapPanTool");
            itemUidList.Add("esriControls.ControlsMapFullExtentCommand");
            itemUidList.Add("esriControls.ControlsMapIdentifyTool");
            itemUidList.Add("esriControls.ControlsSelectFeaturesTool");
            itemUidList.Add("esriControls.ControlsClearSelectionCommand");
            foreach (string uidValue in itemUidList)
            {
                var uid = new UIDClass();
                uid.Value = uidValue;
                mapToolbar.AddItem(uid, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
            }
        }

        private void WireMapEvents()
        {
            mapControl.OnMouseMove += new IMapControlEvents2_Ax_OnMouseMoveEventHandler(mapControl_OnMouseMove);
            mapControl.OnMouseDown += new IMapControlEvents2_Ax_OnMouseDownEventHandler(mapControl_OnMouseDown);
        }

        private void mapControl_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            mapControl.Focus();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SetMapProperties();
            WireMapEvents();
            mapControl.LoadMxFile(@"E:\project\GeoProgram\ArcEngine_1\ArcEngine_1\bin\Debug\map.mxd");
            fullExtent_Click(null,null);//SetToolbarItems();
        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Application.Current.Shutdown();
        }

        private void user_Click(object sender, RoutedEventArgs e)
        {
            var newWindow = new Window1();
            newWindow.ShowDialog();
        }

        private void ItemQuery_Click(object sender, RoutedEventArgs e)
        {
            var newWindow = new WindowItemQuery();
            newWindow.ShowDialog();
        }

        private void BidHistory_Click(object sender, RoutedEventArgs e)
        {
            var newWindow = new WindowMyBids();
            newWindow.ShowDialog();
        }

        private void Favorite_Click(object sender, RoutedEventArgs e)
        {
            var newWindow = new WindowFavorite();
            newWindow.ShowDialog();
        }

        private void NearbyQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        private void PostItem_Click(object sender, RoutedEventArgs e)
        {
            Window newWindow = new WindowPostItem();
            MainWindow.coworkingWnd = newWindow;
            newWindow.ShowDialog();
        }

        private void Shelf_Click(object sender, RoutedEventArgs e)
        {
            var newWindow = new WindowShelf();
            newWindow.Show();
        }

        private void mapControl_OnMouseDown(Object sender, IMapControlEvents2_OnMouseDownEvent e )
        {
            switch (mapControlStatus)
            {
                case 0:
                    mapControl.Focus();
                    if (pan.IsChecked.HasValue && pan.IsChecked.Value)
                        mapControl.Pan();
                        break;
                case 1:
                    if (MainWindow.coworkingWnd != null)
                    {
                        WindowPostItem wpi = MainWindow.coworkingWnd as WindowPostItem;
                        wpi.vm.goods.Goods_latitude = e.mapY;
                        wpi.vm.goods.Goods_longitude = e.mapX;
                        if (wpi.Owner is WindowShelf)
                            wpi.Owner.Show();
                        coworkingWnd.Show();
                        MainWindow.mapControlStatus = 0;
                    }break;
            }
        }

        private void help_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(@"E:\project\任务计划书.pdf");
        }

        private void about_Click(object sender, RoutedEventArgs e)
        {
            WindowAbout about = new WindowAbout();
            about.ShowDialog();
        }

        private void fullExtent_Click(object sender, RoutedEventArgs e)
        {
            mapControl.Extent = mapControl.FullExtent;
        }

        private void admin_Click(object sender, RoutedEventArgs e)
        {
            if (toolbarHost.Child == null)
            {
                CreateMapToolbarControl();
                SetToolbarItems();
            }
            if (tocHost.Child == null)
            {
                CreateTocControl();
            }
            switch (admin.IsChecked.Value)
            { 
                case true:
                    groupBoxBuy.Visibility = Visibility.Collapsed;
                    groupBoxSell.Visibility = Visibility.Collapsed;
                    spAdmin.Visibility = Visibility.Visible;
                    break;
                case false:
                    groupBoxBuy.Visibility = Visibility.Visible;
                    groupBoxSell.Visibility = Visibility.Visible;
                    spAdmin.Visibility = Visibility.Collapsed;
                    break;
            }
        }

        private void listAll_Click(object sender, RoutedEventArgs e)
        {
            WindowShelf shf = new WindowShelf();
            shf.ShowDialog();
        }
    }
}
